﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;

namespace Dragon
{
    public partial class wfKPIMonitor : System.Web.UI.Page
    {
        DataService ds = new DataService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtFromDate.Text = "01/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("yyyy");
                this.txtToDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
                this.GridView1.Visible = false;
            }
        }

        protected void btnRunReport_Click(object sender, EventArgs e)
        {
            DateTime fromDate = new DateTime();
            DateTime toDate = new DateTime();

            try
            {
                fromDate = DateTime.ParseExact(this.txtFromDate.Text, "dd/MM/yyyy", null);
                toDate = DateTime.ParseExact(this.txtToDate.Text, "dd/MM/yyyy", null);
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "<script>alert('Ngày tháng không hợp lệ!');</script>");
                return;
            }

            string provinceCode = "";
            if (this.ddlProvince.SelectedIndex != 0)
                provinceCode = this.ddlProvince.SelectedValue.ToString();

            string userCode = "";
            if (this.ddlCVTCSR.SelectedIndex != 0)
                userCode = this.ddlCVTCSR.SelectedValue.ToString();

            string sql = "exec sp_Report_KPI_Monitor @FromDate, @ToDate, @ProvinceCode, @UserCode";
            SqlParameter pFromDate = new SqlParameter("@FromDate", fromDate);
            SqlParameter pToDate = new SqlParameter("@ToDate", toDate);
            SqlParameter pProvinceCode = new SqlParameter("@ProvinceCode", provinceCode);
            SqlParameter pUserCode = new SqlParameter("@UserCode", userCode);
            ds.SetData(sql, pFromDate, pToDate, pProvinceCode, pUserCode);

            this.GridView1.Visible = true;
            this.GridView1.DataBind();
        }

        protected void ProvinceOnDataBound(object sender, EventArgs e)
        {
            this.ddlProvince.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlProvince.SelectedIndex = 0;
        }

        protected void ddlCVTCSR_DataBound(object sender, EventArgs e)
        {
            this.ddlCVTCSR.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlCVTCSR.SelectedIndex = 0;
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=KPIMonitor.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);

                //To Export all pages
                GridView1.AllowPaging = false;
                this.GridView1.DataBind();

                GridView1.HeaderRow.BackColor = Color.White;
                foreach (TableCell cell in GridView1.HeaderRow.Cells)
                {
                    cell.BackColor = GridView1.HeaderStyle.BackColor;
                }
                foreach (GridViewRow row in GridView1.Rows)
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        if (row.RowIndex % 2 == 0)
                        {
                            cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                        }
                        else
                        {
                            cell.BackColor = GridView1.RowStyle.BackColor;
                        }
                        cell.CssClass = "textmode";
                    }
                }

                GridView1.RenderControl(hw);

                //style to format numbers to string
                string style = @"<style> .textmode { } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /*Tell the compiler that the control is rendered
             * explicitly by overriding the VerifyRenderingInServerForm event.*/
        }

        protected void btnUpdateKPI_Click(object sender, EventArgs e)
        {
            DateTime fromDate = new DateTime();
            DateTime toDate = new DateTime();

            try
            {
                fromDate = DateTime.ParseExact(this.txtFromDate.Text, "dd/MM/yyyy", null);
                toDate = DateTime.ParseExact(this.txtToDate.Text, "dd/MM/yyyy", null);
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "<script>alert('Ngày tháng không hợp lệ!');</script>");
                return;
            }

            string sql = "exec sp_Update_KPI_Monitor @FromDate, @ToDate";
            SqlParameter pFromDate = new SqlParameter("@FromDate", fromDate);
            SqlParameter pToDate = new SqlParameter("@ToDate", toDate);            
            ds.SetData(sql, pFromDate, pToDate);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "<script>alert('Cập nhật dữ liệu thành công!');</script>");
        }
    }
}